﻿Imports System.Data.Entity
Imports MongoDB.Driver
Imports MongoDB.Driver.Builders
Imports MongoDB.Bson

Public MustInherit Class MongoDBInit(Of TEntity As Class)
    Private srv As String = "localhost"
    Private db As String = "mDemo"
    Private uName As String = ""
    Private pwd As String = ""

    Private ConnectionString = String.Format("server={0};database={1};safe=true;username={2};Password={3}", srv, db, uName, pwd)

    Private mBuilder = New MongoConnectionStringBuilder(ConnectionString)
    Private mServer As MongoServer = MongoServer.Create(mBuilder)
    Private mDB As MongoDatabase = mServer(db)

    Friend ReadOnly Collection As MongoCollection(Of TEntity) = mDB.GetCollection(Of TEntity)(GetType(TEntity).Name)

    Friend mResult As WriteConcernResult
End Class

Public Class MongoDbSet(Of TEntity As Class)
    Inherits MongoDBInit(Of TEntity)
    Implements IDbSet(Of TEntity)


    Public Function GetEnumerator() As IEnumerator(Of TEntity) Implements IEnumerable(Of TEntity).GetEnumerator

    End Function

    Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator

    End Function

    Public Function Add(entity As TEntity) As TEntity Implements IDbSet(Of TEntity).Add
        Try
            mResult = Collection.Insert(entity, SafeMode.True)
        Catch generatedExceptionName As MongoConnectionException
            'Hata logları.
            Return Nothing
        End Try
        Return entity
    End Function

    Public Function Attach(entity As TEntity) As TEntity Implements IDbSet(Of TEntity).Attach

    End Function

    Public Function Create() As TEntity Implements IDbSet(Of TEntity).Create

    End Function

    Public Function Create(Of TDerivedEntity As {Class, TEntity})() As TDerivedEntity Implements IDbSet(Of TEntity).Create

    End Function

    Public Function Find(ParamArray keyValues() As Object) As TEntity Implements IDbSet(Of TEntity).Find

    End Function

    Public ReadOnly Property Local As ObjectModel.ObservableCollection(Of TEntity) Implements IDbSet(Of TEntity).Local
        Get

        End Get
    End Property

    Public Function Remove(entity As TEntity) As TEntity Implements IDbSet(Of TEntity).Remove

    End Function

    Public ReadOnly Property ElementType As Type Implements IQueryable.ElementType
        Get

        End Get
    End Property

    Public ReadOnly Property Expression As Expressions.Expression Implements IQueryable.Expression
        Get

        End Get
    End Property

    Public ReadOnly Property Provider As IQueryProvider Implements IQueryable.Provider
        Get

        End Get
    End Property
End Class

Public Interface IMongoEntity
    Property _id As ObjectId
End Interface

Public Class MongoEntity
    Implements IMongoEntity
    Public Property _id As ObjectId Implements IMongoEntity._id
    Sub New()
        _id = ObjectId.GenerateNewId
    End Sub
End Class